package com.bank.ui.customer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import com.bank.dao.CardDAO;

public class DepositPanel extends JPanel {
    private JTextField cardIDField;
    private JTextField amountField;
    private JPasswordField passwordField;
    private JButton depositButton;
    private JLabel messageLabel;
    private final CardDAO cardDAO = new CardDAO();

    public DepositPanel() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);

        // 卡号输入框
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.EAST;
        add(new JLabel("卡号:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        cardIDField = new JTextField(16);
        add(cardIDField, gbc);

        // 金额输入框
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.EAST;
        add(new JLabel("金额:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        amountField = new JTextField(10);
        add(amountField, gbc);

        // 密码输入框
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.EAST;
        add(new JLabel("密码:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        passwordField = new JPasswordField(20);
        add(passwordField, gbc);

        // 存款按钮
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        depositButton = new JButton("存款");
        add(depositButton, gbc);

        // 消息标签
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        messageLabel = new JLabel("", JLabel.CENTER);
        add(messageLabel, gbc);

        // 添加事件监听器
        depositButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                performDeposit();
            }
        });
    }

    // 执行存款操作
    private void performDeposit() {
        String cardID = cardIDField.getText();
        String amountStr = amountField.getText();
        String password = new String(passwordField.getPassword());

        // 验证输入
        if (cardID.isEmpty() || amountStr.isEmpty() || password.isEmpty()) {
            messageLabel.setText("卡号、金额和密码不能为空！");
            messageLabel.setForeground(Color.RED);
            return;
        }

        try {
            double amount = Double.parseDouble(amountStr);
            if (amount <= 0) {
                messageLabel.setText("金额必须大于0！");
                messageLabel.setForeground(Color.RED);
                return;
            }

            // 检查卡是否挂失
            if (cardDAO.isCardReportedLoss(cardID)) {
                messageLabel.setText("存款失败：该卡已挂失！");
                messageLabel.setForeground(Color.RED);
                return;
            }

            // 执行存款操作
            boolean success = cardDAO.deposit(cardID, password, amount);
            if (success) {
                messageLabel.setText("存款成功！");
                messageLabel.setForeground(Color.GREEN);
                // 清空输入字段
                amountField.setText("");
                passwordField.setText("");
            } else {
                messageLabel.setText("存款失败，请检查卡号和密码！");
                messageLabel.setForeground(Color.RED);
            }
        } catch (NumberFormatException e) {
            messageLabel.setText("请输入有效的金额！");
            messageLabel.setForeground(Color.RED);
        } catch (SQLException e) {
            e.printStackTrace();
            messageLabel.setText("数据库错误，请稍后再试！");
            messageLabel.setForeground(Color.RED);
        }
    }
}